<?php namespace Modules\Shop\Http\Controllers;


class MessageController extends Controller {

    public function getIndex()
    {
        global $_CFG, $ecs, $err, $smarty, $_LANG, $db;

        if (empty($_CFG['message_board'])) {
            show_message($_LANG['message_board_close']);
        }
        $action = is_set(\Input::get('act')) ? trim(\Input::get('act')) : 'default';
        if ($action == 'act_add_message') {
            //include_once(ROOT_PATH . 'includes/lib_clips.php');

            /* 验证码防止灌水刷屏 */
            if ((intval($_CFG['captcha']) & CAPTCHA_MESSAGE) && gd_version() > 0) {
                //include_once('includes/cls_captcha.php');
                $validator = new \captcha();
                if (!$validator->check_word($_POST['captcha'])) {
                    show_message($_LANG['invalid_captcha']);
                }
            } else {
                /* 没有验证码时，用时间来限制机器人发帖或恶意发评论 */
                if (!isset($_SESSION['send_time'])) {
                    $_SESSION['send_time'] = 0;
                }

                $cur_time = gmtime();
                if (($cur_time - $_SESSION['send_time']) < 30) // 小于30秒禁止发评论
                {
                    show_message($_LANG['cmt_spam_warning']);
                }
            }
            $user_name = '';
            if (empty($_POST['anonymous']) && !empty($_SESSION['user_name'])) {
                $user_name = $_SESSION['user_name'];
            } elseif (!empty($_POST['anonymous']) && !isset($_POST['user_name'])) {
                $user_name = $_LANG['anonymous'];
            } elseif (empty($_POST['user_name'])) {
                $user_name = $_LANG['anonymous'];
            } else {
                $user_name = htmlspecialchars(trim($_POST['user_name']));
            }

            $user_id = !empty($_SESSION['user_id']) ? $_SESSION['user_id'] : 0;
            $message = array(
                'user_id'     => $user_id,
                'user_name'   => $user_name,
                'user_email'  => isset($_POST['user_email']) ? htmlspecialchars(trim($_POST['user_email'])) : '',
                'msg_type'    => isset($_POST['msg_type']) ? intval($_POST['msg_type']) : 0,
                'msg_title'   => isset($_POST['msg_title']) ? trim($_POST['msg_title']) : '',
                'msg_content' => isset($_POST['msg_content']) ? trim($_POST['msg_content']) : '',
                'order_id'    => 0,
                'msg_area'    => 1,
                'upload'      => array()
            );

            if (add_message($message)) {
                if (intval($_CFG['captcha']) & CAPTCHA_MESSAGE) {
                    unset($_SESSION[ $validator->session_word ]);
                } else {
                    $_SESSION['send_time'] = $cur_time;
                }
                $msg_info = $_CFG['message_check'] ? $_LANG['message_submit_wait'] : $_LANG['message_submit_done'];
                show_message($msg_info, $_LANG['message_list_lnk'], 'message.php');
            } else {
                $err->show($_LANG['message_list_lnk'], 'message.php');
            }
        }

        if ($action == 'default') {
            assign_template();
            $position = assign_ur_here(0, $_LANG['message_board']);
            \Tpl::assign('page_title', $position['title']);    // 页面标题
            \Tpl::assign('ur_here', $position['ur_here']);  // 当前位置
            \Tpl::assign('helps', get_shop_help());       // 网店帮助

            \Tpl::assign('categories', get_categories_tree()); // 分类树
            \Tpl::assign('top_goods', get_top10());           // 销售排行
            \Tpl::assign('cat_list', cat_list(0, 0, true, 2, false));
            \Tpl::assign('brand_list', get_brand_list());
            \Tpl::assign('promotion_info', get_promotion_info());

            \Tpl::assign('enabled_mes_captcha', (intval($_CFG['captcha']) & CAPTCHA_MESSAGE));

            $sql = "SELECT COUNT(*) FROM " . \ECS::table('comment') . " WHERE STATUS =1 AND comment_type =0 ";
            $record_count = $db->getOne($sql);
            $sql = "SELECT COUNT(*) FROM " . \ECS::table('feedback') . " WHERE `msg_area`='1' AND `msg_status` = '1' ";
            $record_count += $db->getOne($sql);

            /* 获取留言的数量 */
            $page = is_set(\Input::get('page')) ? intval(\Input::get('page')) : 1;
            $pagesize = get_library_number('message_list', 'message_board');
            $pager = get_pager('message.php', array(), $record_count, $page, $pagesize);
            $msg_lists = get_msg_list($pagesize, $pager['start']);
            assign_dynamic('message_board');
            \Tpl::assign('rand', mt_rand());
            \Tpl::assign('msg_lists', $msg_lists);
            \Tpl::assign('pager', $pager);
            \Tpl::display('message_board.dwt');
        }

        return view('shop::index');
	}
	
}